

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.mail &mdash; Cup 3.2.19 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 

  
  <script src="../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../index.html" class="icon icon-home"> Cup
          

          
          </a>

          
            
            
              <div class="version">
                3.2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../cup.html">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>cup.mail</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.mail</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    mail related modules.</span>

<span class="sd">    **Recommand using SmtpMailer**</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;mutt_sendmail&#39;</span><span class="p">,</span> <span class="s1">&#39;SmtpMailer&#39;</span><span class="p">]</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">import</span> <span class="nn">mimetypes</span>
<span class="kn">import</span> <span class="nn">smtplib</span>
<span class="kn">from</span> <span class="nn">email.mime</span> <span class="k">import</span> <span class="n">multipart</span>
<span class="kn">from</span> <span class="nn">email</span> <span class="k">import</span> <span class="n">encoders</span>
<span class="kn">from</span> <span class="nn">email.mime</span> <span class="k">import</span> <span class="n">audio</span>
<span class="kn">from</span> <span class="nn">email.mime</span> <span class="k">import</span> <span class="n">base</span>
<span class="kn">from</span> <span class="nn">email.mime</span> <span class="k">import</span> <span class="n">image</span>
<span class="kn">from</span> <span class="nn">email.mime</span> <span class="k">import</span> <span class="n">text</span>

<span class="kn">import</span> <span class="nn">cup</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">log</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">shell</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="k">import</span> <span class="n">decorators</span>


<div class="viewcode-block" id="mutt_sendmail"><a class="viewcode-back" href="../../cup.html#cup.mail.mutt_sendmail">[docs]</a><span class="k">def</span> <span class="nf">mutt_sendmail</span><span class="p">(</span>  <span class="c1"># pylint: disable=R0913,W0613</span>
    <span class="n">tostr</span><span class="p">,</span> <span class="n">subject</span><span class="p">,</span> <span class="n">body</span><span class="p">,</span> <span class="n">attach</span><span class="p">,</span> <span class="n">content_is_html</span><span class="o">=</span><span class="kc">False</span>
<span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Plz notice this function is not recommanded to use. Use SmtpMailer instead.</span>

<span class="sd">    :param  exec_cwd:</span>
<span class="sd">        exec working directory. Plz use</span>
<span class="sd">    :param tostr:</span>
<span class="sd">        recipt list, separated by ,</span>
<span class="sd">    :param subject:</span>
<span class="sd">        subject</span>
<span class="sd">    :param body:</span>
<span class="sd">        email content</span>
<span class="sd">    :param attach:</span>
<span class="sd">        email attachment</span>
<span class="sd">    :param content_is_html:</span>
<span class="sd">        is htm mode opened</span>
<span class="sd">    :return:</span>
<span class="sd">        return True on success, False otherwise</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">decorators</span><span class="o">.</span><span class="n">needlinux</span><span class="p">(</span><span class="n">mutt_sendmail</span><span class="p">)</span>
    <span class="n">shellobj</span> <span class="o">=</span> <span class="n">shell</span><span class="o">.</span><span class="n">ShellExec</span><span class="p">()</span>
    <span class="n">temp_cwd</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>

    <span class="n">str_att</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="n">cmdstr</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="k">if</span> <span class="n">attach</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">content_is_html</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">cmdstr</span> <span class="o">=</span> <span class="s1">&#39;echo &quot;</span><span class="si">%s</span><span class="s1">&quot;|/usr/bin/mutt -e &quot;my_hdr Content-Type:&#39;</span>\
                <span class="s1">&#39;text/html&quot; -s &quot;</span><span class="si">%s</span><span class="s1">&quot; </span><span class="si">%s</span><span class="s1">&#39;</span> \
                <span class="o">%</span> <span class="p">(</span><span class="n">body</span><span class="p">,</span> <span class="n">subject</span><span class="p">,</span> <span class="n">tostr</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cmdstr</span> <span class="o">=</span> <span class="s1">&#39;echo &quot;</span><span class="si">%s</span><span class="s1">&quot;|/usr/bin/mutt -s &quot;</span><span class="si">%s</span><span class="s1">&quot; </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">body</span><span class="p">,</span> <span class="n">subject</span><span class="p">,</span> <span class="n">tostr</span>
            <span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">attlist</span> <span class="o">=</span> <span class="n">attach</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span>
        <span class="n">attlen</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">attlist</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">attlen</span><span class="p">):</span>
            <span class="n">str_att</span> <span class="o">+=</span> <span class="s1">&#39;-a &#39;</span> <span class="o">+</span> <span class="n">attlist</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
            <span class="k">if</span><span class="p">(</span><span class="n">i</span> <span class="o">!=</span> <span class="n">attlen</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
                <span class="n">str_att</span> <span class="o">+=</span> <span class="s1">&#39; &#39;</span>
        <span class="k">if</span> <span class="n">content_is_html</span> <span class="ow">is</span> <span class="kc">True</span><span class="p">:</span>
            <span class="n">cmdstr</span> <span class="o">=</span> <span class="s1">&#39;echo </span><span class="si">%s</span><span class="s1">|/usr/bin/mutt -e &quot;my_hdr Content-Type:&#39;</span>\
                <span class="s1">&#39;text/html&quot; -s &quot;</span><span class="si">%s</span><span class="s1">&quot; </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">body</span><span class="p">,</span> <span class="n">subject</span><span class="p">,</span> <span class="n">str_att</span><span class="p">,</span> <span class="n">tostr</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cmdstr</span> <span class="o">=</span> <span class="s1">&#39;echo </span><span class="si">%s</span><span class="s1">|/usr/bin/mutt -s &quot;</span><span class="si">%s</span><span class="s1">&quot; </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
                <span class="n">body</span><span class="p">,</span> <span class="n">subject</span><span class="p">,</span> <span class="n">str_att</span><span class="p">,</span> <span class="n">tostr</span>
            <span class="p">)</span>
    <span class="n">ret_dic</span> <span class="o">=</span> <span class="n">shellobj</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">cmdstr</span><span class="p">,</span> <span class="mi">60</span><span class="p">)</span>
    <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">temp_cwd</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">ret_dic</span><span class="p">[</span><span class="s1">&#39;returncode&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">True</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">ret_dic</span><span class="p">[</span><span class="s1">&#39;stderr&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="SmtpMailer"><a class="viewcode-back" href="../../cup.html#cup.mail.SmtpMailer">[docs]</a><span class="k">class</span> <span class="nc">SmtpMailer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>  <span class="c1"># pylint: disable=R0903</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    :param sender:  mail sender</span>
<span class="sd">    :param server: smtp的mailserver</span>
<span class="sd">    :param port: port</span>
<span class="sd">    :param is_html:  is html enabled</span>

<span class="sd">    smtp server examples</span>
<span class="sd">    ::</span>

<span class="sd">        from cup import mail</span>
<span class="sd">        mailer = mail.SmtpMailer(</span>
<span class="sd">            &#39;xxx@xxx.com&#39;,</span>
<span class="sd">            &#39;xxxx.smtp.xxx.com&#39;,</span>
<span class="sd">            is_html=True</span>
<span class="sd">        )</span>
<span class="sd">        # if your smtp server needs login , pls uncomment line below:</span>
<span class="sd">        # mailer.login(usernname, password)</span>
<span class="sd">        mailer.sendmail(</span>
<span class="sd">            [</span>
<span class="sd">                &#39;maguannan&#39;,</span>
<span class="sd">                &#39;liuxuan05&#39;,</span>
<span class="sd">                &#39;zhaominghao&#39;</span>
<span class="sd">            ],</span>
<span class="sd">            &#39;test_img&#39;,</span>
<span class="sd">            (</span>
<span class="sd">                &#39;testset &lt;img src=&quot;cid:screenshot.png&quot;&gt;&lt;/img&gt;&#39;</span>
<span class="sd">            ),</span>
<span class="sd">            [</span>
<span class="sd">                &#39;/home/work/screenshot.png&#39;,</span>
<span class="sd">                &#39;../abc.zip&#39;</span>
<span class="sd">            ]</span>
<span class="sd">        )</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_COMMA_SPLITTER</span> <span class="o">=</span> <span class="s1">&#39;,&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">server</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">25</span><span class="p">,</span>\
        <span class="n">is_html</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>

<span class="sd">        :param sender:</span>
<span class="sd">            sender email address,  xxx@xxx.com</span>
<span class="sd">        :param server:</span>
<span class="sd">            smtp server</span>
<span class="sd">        :param port:</span>
<span class="sd">            25 by default</span>
<span class="sd">        :param is_html:</span>
<span class="sd">            is email format a html style, False by default.</span>
<span class="sd">            You can set it to True if the email format is html based.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_server</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_port</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sender</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_is_html</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_login_params</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setup</span><span class="p">(</span><span class="n">sender</span><span class="p">,</span> <span class="n">server</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">is_html</span><span class="p">)</span>

<div class="viewcode-block" id="SmtpMailer.setup"><a class="viewcode-back" href="../../cup.html#cup.mail.SmtpMailer.setup">[docs]</a>    <span class="k">def</span> <span class="nf">setup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sender</span><span class="p">,</span> <span class="n">server</span><span class="p">,</span> <span class="n">port</span><span class="o">=</span><span class="mi">25</span><span class="p">,</span> <span class="n">is_html</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        change parameters during run-time</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_server</span> <span class="o">=</span> <span class="n">server</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_port</span> <span class="o">=</span> <span class="n">port</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sender</span> <span class="o">=</span> <span class="n">sender</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_is_html</span> <span class="o">=</span> <span class="n">is_html</span></div>

<div class="viewcode-block" id="SmtpMailer.login"><a class="viewcode-back" href="../../cup.html#cup.mail.SmtpMailer.login">[docs]</a>    <span class="k">def</span> <span class="nf">login</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">username</span><span class="p">,</span> <span class="n">passwords</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        if the smtp need login, plz call this method before you call</span>
<span class="sd">        sendmail</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;smtp server will login with user </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">username</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_login_params</span> <span class="o">=</span> <span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="n">passwords</span><span class="p">)</span></div>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_check_type</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">instance</span><span class="p">,</span> <span class="n">type_list</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">type</span><span class="p">(</span><span class="n">instance</span><span class="p">)</span> <span class="ow">in</span> <span class="n">type_list</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> only accepts types like: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                <span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">type_list</span><span class="p">))</span>
            <span class="p">)</span>

    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">_handle_attachments</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">outer</span><span class="p">,</span> <span class="n">attachments</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">attachments</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
            <span class="n">attrs</span> <span class="o">=</span> <span class="p">[</span><span class="n">attachments</span><span class="p">]</span>
        <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">attachments</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">:</span>
            <span class="n">attrs</span> <span class="o">=</span> <span class="n">attachments</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">attrs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">attached</span> <span class="ow">in</span> <span class="n">attrs</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">attached</span><span class="p">):</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;attached is not a file:</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">attached</span><span class="p">)</span>
                <span class="k">continue</span>
            <span class="c1"># Guess the content type based on the file&#39;s extension.  Encoding</span>
            <span class="c1"># will be ignored, although we should check for simple things like</span>
            <span class="c1"># gzip&#39;d or compressed files.</span>
            <span class="n">ctype</span><span class="p">,</span> <span class="n">encoding</span> <span class="o">=</span> <span class="n">mimetypes</span><span class="o">.</span><span class="n">guess_type</span><span class="p">(</span><span class="n">attached</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ctype</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">encoding</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="c1"># No guess could be made, or the file is encoded (compressed)</span>
                <span class="c1"># use a generic bag-of-bits type.</span>
                <span class="n">ctype</span> <span class="o">=</span> <span class="s1">&#39;application/octet-stream&#39;</span>
            <span class="n">maintype</span><span class="p">,</span> <span class="n">subtype</span> <span class="o">=</span> <span class="n">ctype</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">maintype</span> <span class="o">==</span> <span class="s1">&#39;text&#39;</span><span class="p">:</span>
                    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">attached</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fhandle</span><span class="p">:</span>
                        <span class="c1"># Note: we should handle calculating the charset</span>
                        <span class="n">msg</span> <span class="o">=</span> <span class="n">text</span><span class="o">.</span><span class="n">MIMEText</span><span class="p">(</span>
                            <span class="n">fhandle</span><span class="o">.</span><span class="n">read</span><span class="p">(),</span> <span class="n">_subtype</span><span class="o">=</span><span class="n">subtype</span>
                        <span class="p">)</span>
                <span class="k">elif</span> <span class="n">maintype</span> <span class="o">==</span> <span class="s1">&#39;image&#39;</span><span class="p">:</span>
                    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">attached</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fhandle</span><span class="p">:</span>
                        <span class="n">imgid</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">attached</span><span class="p">)</span>
                        <span class="n">msg</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="n">MIMEImage</span><span class="p">(</span>
                            <span class="n">fhandle</span><span class="o">.</span><span class="n">read</span><span class="p">(),</span> <span class="n">_subtype</span><span class="o">=</span><span class="n">subtype</span>
                        <span class="p">)</span>
                        <span class="n">msg</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="s1">&#39;Content-ID&#39;</span><span class="p">,</span> <span class="n">imgid</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">maintype</span> <span class="o">==</span> <span class="s1">&#39;audio&#39;</span><span class="p">:</span>
                    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">attached</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fhandle</span><span class="p">:</span>
                        <span class="n">msg</span> <span class="o">=</span> <span class="n">audio</span><span class="o">.</span><span class="n">MIMEAudio</span><span class="p">(</span><span class="n">fhandle</span><span class="o">.</span><span class="n">read</span><span class="p">(),</span> <span class="n">_subtype</span><span class="o">=</span><span class="n">subtype</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">attached</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fhandle</span><span class="p">:</span>
                        <span class="n">msg</span> <span class="o">=</span> <span class="n">base</span><span class="o">.</span><span class="n">MIMEBase</span><span class="p">(</span><span class="n">maintype</span><span class="p">,</span> <span class="n">subtype</span><span class="p">)</span>
                        <span class="n">msg</span><span class="o">.</span><span class="n">set_payload</span><span class="p">(</span><span class="n">fhandle</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
                    <span class="c1"># Encode the payload using Base64</span>
                    <span class="n">encoders</span><span class="o">.</span><span class="n">encode_base64</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                    <span class="c1"># Set the filename parameter</span>
                <span class="n">msg</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span>
                    <span class="s1">&#39;Content-Disposition&#39;</span><span class="p">,</span> <span class="s1">&#39;attachment&#39;</span><span class="p">,</span>
                    <span class="n">filename</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">attached</span><span class="p">)</span>
                <span class="p">)</span>
                <span class="n">outer</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="c1"># pylint: disable=W0703</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">exception</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                    <span class="s1">&#39;failed to attach </span><span class="si">%s</span><span class="s1">, errmsg:</span><span class="si">%s</span><span class="s1">. Will skip it&#39;</span> <span class="o">%</span> <span class="p">(</span>
                        <span class="n">attached</span><span class="p">,</span> <span class="nb">str</span><span class="p">(</span><span class="n">exception</span><span class="p">)</span>
                    <span class="p">)</span>
                <span class="p">)</span>

    <span class="c1"># pylint: disable=R0914,R0912,R0915</span>
<div class="viewcode-block" id="SmtpMailer.sendmail"><a class="viewcode-back" href="../../cup.html#cup.mail.SmtpMailer.sendmail">[docs]</a>    <span class="k">def</span> <span class="nf">sendmail</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">recipients</span><span class="p">,</span> <span class="n">subject</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">attachments</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">cc</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">bcc</span><span class="o">=</span><span class="kc">None</span>
    <span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        send mail</span>

<span class="sd">        :param recipients:</span>
<span class="sd">            &quot;list&quot; of recipients. See the example above</span>
<span class="sd">        :param subject:</span>
<span class="sd">            subject</span>
<span class="sd">        :param body:</span>
<span class="sd">            body of the mail</span>
<span class="sd">        :param attachments:</span>
<span class="sd">            &quot;list&quot; of attachments. Plz use absolute file path!</span>
<span class="sd">        :param cc:</span>
<span class="sd">            cc list</span>
<span class="sd">        :param bcc:</span>
<span class="sd">            bcc list</span>
<span class="sd">        :return:</span>
<span class="sd">            return (True, None) on success, return (False, error_msg) otherwise</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">toaddrs</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="c1"># self._check_type(body, [str])</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_is_html</span><span class="p">:</span>
            <span class="n">msg_body</span> <span class="o">=</span> <span class="n">text</span><span class="o">.</span><span class="n">MIMEText</span><span class="p">(</span><span class="n">body</span><span class="p">,</span> <span class="s1">&#39;html&#39;</span><span class="p">,</span> <span class="n">_charset</span><span class="o">=</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg_body</span> <span class="o">=</span> <span class="n">text</span><span class="o">.</span><span class="n">MIMEText</span><span class="p">(</span><span class="n">body</span><span class="p">,</span> <span class="s1">&#39;plain&#39;</span><span class="p">,</span> <span class="n">_charset</span><span class="o">=</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
        <span class="n">outer</span> <span class="o">=</span> <span class="n">multipart</span><span class="o">.</span><span class="n">MIMEMultipart</span><span class="p">()</span>
        <span class="n">outer</span><span class="p">[</span><span class="s1">&#39;Subject&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">subject</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">recipients</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
            <span class="n">outer</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_COMMA_SPLITTER</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">recipients</span><span class="p">)</span>
            <span class="n">toaddrs</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">recipients</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">outer</span><span class="p">[</span><span class="s1">&#39;To&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">recipients</span>
            <span class="n">toaddrs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">recipients</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">cc</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">any</span><span class="p">([</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">bcc</span><span class="p">,</span> <span class="nb">str</span><span class="p">),</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">bcc</span><span class="p">,</span> <span class="n">unicode</span><span class="p">)]):</span>
                <span class="n">outer</span><span class="p">[</span><span class="s1">&#39;Cc&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cc</span>
                <span class="n">toaddrs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">cc</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cc</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                <span class="n">outer</span><span class="p">[</span><span class="s1">&#39;Cc&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_COMMA_SPLITTER</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">cc</span><span class="p">)</span>
                <span class="n">toaddrs</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">cc</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;cc only accepts string or list&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">bcc</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">any</span><span class="p">([</span>
                    <span class="nb">isinstance</span><span class="p">(</span><span class="n">bcc</span><span class="p">,</span> <span class="nb">str</span><span class="p">),</span>
                    <span class="nb">isinstance</span><span class="p">(</span><span class="n">bcc</span><span class="p">,</span> <span class="n">unicode</span><span class="p">),</span>
            <span class="p">]):</span>
                <span class="n">outer</span><span class="p">[</span><span class="s1">&#39;Bcc&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">bcc</span>
                <span class="n">toaddrs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bcc</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">bcc</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                <span class="n">outer</span><span class="p">[</span><span class="s1">&#39;Bcc&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_COMMA_SPLITTER</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">bcc</span><span class="p">)</span>
                <span class="n">toaddrs</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">bcc</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;bcc only accepts string or list&#39;</span><span class="p">)</span>
        <span class="n">outer</span><span class="p">[</span><span class="s1">&#39;From&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sender</span>
        <span class="n">outer</span><span class="o">.</span><span class="n">preamble</span> <span class="o">=</span> <span class="s1">&#39;Peace and Joy!</span><span class="se">\n</span><span class="s1">&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_handle_attachments</span><span class="p">(</span><span class="n">outer</span><span class="p">,</span> <span class="n">attachments</span><span class="p">)</span>
        <span class="n">outer</span><span class="o">.</span><span class="n">attach</span><span class="p">(</span><span class="n">msg_body</span><span class="p">)</span>
        <span class="c1"># handle attachments</span>
        <span class="n">composed</span> <span class="o">=</span> <span class="n">outer</span><span class="o">.</span><span class="n">as_string</span><span class="p">()</span>
        <span class="n">ret</span> <span class="o">=</span> <span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="s1">&#39;failed to send email&#39;</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">smtp</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_server</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_port</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_login_params</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">smtp</span><span class="o">.</span><span class="n">login</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_login_params</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">_login_params</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
            <span class="n">smtp</span><span class="o">.</span><span class="n">sendmail</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_sender</span><span class="p">,</span> <span class="n">toaddrs</span><span class="p">,</span> <span class="n">composed</span><span class="p">)</span>
            <span class="n">smtp</span><span class="o">.</span><span class="n">quit</span><span class="p">()</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTPException</span> <span class="k">as</span> <span class="n">smtperr</span><span class="p">:</span>
            <span class="n">ret</span> <span class="o">=</span> <span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">smtperr</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">ret</span></div></div>


<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../',
            VERSION:'3.2.19',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../_static/doctools.js"></script>
      <script type="text/javascript" src="../../_static/language_data.js"></script>

  

  <script type="text/javascript" src="../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>